package top.melody.ecommerce.platform.service.authentication.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.melody.ecommerce.platform.EcommerceException;
import top.melody.ecommerce.platform.api.domain.account.AccountEnterpriseAuth;
import top.melody.ecommerce.platform.api.service.AccountAuthService;
import top.melody.ecommerce.platform.dal.account.AccountEnterpriseAuthMapper;

import javax.annotation.Resource;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/11/23 11:07
 * @mail: blentle.huan.ren@gmail.com
 * @description:
 * @since:1.0
 */
@Service
public class AccountAuthServiceImpl implements AccountAuthService {

    private static final Logger logger = LoggerFactory.getLogger(AccountAuthServiceImpl.class);
    @Resource
    private AccountEnterpriseAuthMapper authMapper;

    @Override
    public AccountEnterpriseAuth getAccountEnterpriseAuth(int accountAuthId) throws EcommerceException {
        try {
            return authMapper.get(accountAuthId);
        } catch (Exception e) {
            logger.error("occurred exception when get account enterprise auth entity ", e);
            throw new EcommerceException("get account enterprise authentication error", e);
        }
    }

    @Override
    public AccountEnterpriseAuth getAccountEnterpriseAuthByAccountId(int accountId) throws EcommerceException {
        try {
            return authMapper.getByAccountId(accountId);
        } catch (Exception e) {
            logger.error("occurred exception when get account enterprise auth entity by accountId", e);
            throw new EcommerceException("get account enterprise authentication by accountId error", e);
        }
    }

    @Override
    public int getAccountEnterpriseAuthCount(AccountEnterpriseAuth queryEntity) throws EcommerceException {
        try {
            return authMapper.getCount(queryEntity);
        } catch (Exception e) {
            logger.error("occurred exception when get account enterprise auth count", e);
            throw new EcommerceException("get account enterprise authentication count error", e);
        }
    }

    @Override
    public AccountEnterpriseAuth createAuthentication(AccountEnterpriseAuth enterprise) throws EcommerceException {
        try {
            //todo:Transactional
            //todo: insert authentication ,update account
            authMapper.insert(enterprise);
            //todo:update account
            return enterprise;
        } catch (Exception e) {
            logger.error("occurred exception when get account enterprise auth count", e);
            throw new EcommerceException("get account enterprise authentication count error", e);
        }
    }
}
